/*
 *  This file is part of Fax .NET.
 *
 *  Fax .NET is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation.
 * 
 *  Fax .NET is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Fax .NET.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.Runtime.InteropServices;

namespace Tourreau.Gilles.FaxDotNet
{
    /// <summary>
    /// Controller for print processing to fax printer.
    /// </summary>
    public class FaxController : PrintController
    {
        /// <summary>DC obtaining by Windows Fax Service.</summary>
        private IntPtr dc;

        internal FaxController(IntPtr dc)
            : base()
        {

            this.dc = dc;
        }

        /// <overloads></overloads>
        public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e)
        {
            if (e.Cancel == true)
            {
                NativeMethods.AbortDoc(this.dc);
                return null;
            }

            return Graphics.FromHdc(this.dc);
        }

        /// <overloads></overloads>
        public override void OnEndPage(PrintDocument document, PrintPageEventArgs e)
        {
            if (e.HasMorePages == true)
                NativeMethods.EndPage(this.dc);

            base.OnEndPage(document, e);
        }

        /// <overloads></overloads>
        public override void OnEndPrint(PrintDocument document, PrintEventArgs e)
        {
            if (e.Cancel == false)
                NativeMethods.EndDoc(this.dc);

            base.OnEndPrint(document, e);
        }
    }
}
